<?php

namespace app\payment\admin;

use app\common\builder\ZBuilder;
use app\payment\model\Cardblacklist as CardblacklistModel;

/**
 * 银行卡或支付宝帐号黑名单控制器
 */
class Cardblacklist extends Index
{

    protected function initialize()
    {
        parent::initialize();
        if ($this->no_auth) {
            $this->error('权限不足！');
        }
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        // 设置搜索参数
        $search = ['id' => '银行卡或支付宝帐号'];

        // 获取查询条件
        $map = $this->getMap();

        $list_rows = input('list_rows', 20) >= 20 ? 20 : input('list_rows');
        // 数据列表
        $data_list = CardblacklistModel::where($map)->order('create_time desc')->paginate($list_rows);
        // 分页数据
        $page = $data_list->render();

        $items = [
            ['id', '银行卡或支付宝帐号'],
            ['create_time', '创建时间', 'datetime', '', 'Y-m-d H:i:s'],
        ];

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('银行卡或支付宝帐号黑名单，不允许代付！') // 设置页面标题
            ->setTableName('payment_card_blacklist') // 设置数据表名
            ->setPrimaryKey('id') // 设置主键名为id
            ->setSearch($search) // 设置搜索参数
            ->addColumns($items)
            ->addTopButtons('add,delete') // 批量添加顶部按钮
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    /**
     * 新增
     * @return mixed
     */
    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($this->merchant_info['trade_pwd'] != md5(md5(md5($data['trade_pwd'])))) {
                $this->error('交易密码不正确！');
            }

            unset($data['trade_pwd']);

            if (!$data['id']) {
                $this->error("请输入银行卡或支付宝帐号黑名单！", url('index'));
            }

            $info = CardblacklistModel::where(['id' => $data['id']])->find();
            if ($info) {
                $this->error("请刷新，数据已经存在！");
            }

            if ($temp = CardblacklistModel::create($data)) {
                // 记录行为
                action_log('payment_card_blacklist_add', 'payment_card_blacklist', $temp['id'], UID, json_encode($temp));
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }

        $items = [
            ['text', 'id', '银行卡或支付宝帐号', '银行卡或支付宝帐号黑名单'],
            ['password', 'trade_pwd', '交易密码', '6-16位'],
        ];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增') // 设置页面标题
            ->addFormItems($items)
            ->fetch();
    }

    /**
     * 删除
     * @param array $record 
     * @return mixed
     */
    public function delete($record = [])
    {
        return $this->setStatus('delete');
    }

    /**
     * 启用
     * @param array $record 
     * @return mixed
     */
    public function enable($record = [])
    {
        return $this->setStatus('enable');
    }

    /**
     * 禁用
     * @param array $record 
     * @return mixed
     */
    public function disable($record = [])
    {
        return $this->setStatus('disable');
    }

    /**
     * 设置状态：删除、禁用、启用
     * @param string $type 类型：delete/enable/disable
     * @param array $record 
     * @return mixed
     */
    public function setStatus($type = '', $record = [])
    {
        $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $ids_delete = is_array($ids) ? '' : $ids;
        $data = CardblacklistModel::where('id', 'in', $ids)->column('id');
        return parent::setStatus($type, ['payment_card_blacklist_' . $type, 'payment_card_blacklist', $ids_delete, UID, implode('、', $data)]);
    }

    /**
     * 快速编辑
     * @param array $record 行为日志
     * @return mixed
     */
    public function quickEdit($record = [])
    {
        $id = input('post.pk', '');
        $field = input('post.name', '');
        $value = input('post.value', '');
        $data = CardblacklistModel::where('id', $id)->value($field);
        $details = '字段(' . $field . ')，原值(' . $data . ')，新值：(' . $value . ')';
        return parent::quickEdit(['payment_card_blacklist_edit', 'payment_card_blacklist', $id, UID, $details]);
    }
}
